#include "SDL.h"
#include "SDL_opengl.h"
#include "Square.h"
#include "Cube.h"
#include <string>

#define RESOURCE_DIRECTORY "../Resources/"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 32
#define FRAMES_PER_SECOND 60

using namespace std; 

Square mySquare;
Cube myCube;

bool isFullscreen = false;
bool squareActive = false;
bool cubeActive = false;

void tidyQuit()
{
	SDL_Quit();
}

void toggleFullscreen()
{
	if (!isFullscreen) {
		SDL_SetVideoMode(0, 0, SCREEN_BPP, SDL_SWSURFACE | SDL_FULLSCREEN);
		isFullscreen = true;
	} else {
		SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE);
		isFullscreen = false;
	}
}

int main(int argc, char* args[])
{
	// The images to display on SDL
	SDL_Event sdlEvent;

	// Start SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		fprintf(stderr, "Unable to init SDL %s\n", SDL_GetError());
		exit(1);
	} else {
		SDL_WM_SetCaption("Blitter", NULL);
	}

	// Setup the screen
	SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_SWSURFACE | SDL_RESIZABLE);
	SDL_EnableKeyRepeat(1,1);

	// OpenGL time
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();

	if (glGetError() != GL_NO_ERROR) {
		fprintf(stderr, "There was an error with OpenGL, %s \n", glGetError());
	}
	
	
	SDL_GL_SwapBuffers();
	
	// Main program loop
	while(true) {

		myCube.draw();
		myCube.rotate();
		SDL_GL_SwapBuffers();

		// Check for events sent by SDL
		while(SDL_PollEvent(&sdlEvent)) {

			switch(sdlEvent.type) {
				case SDL_KEYDOWN:
					switch(sdlEvent.key.keysym.sym)
					{
						case SDLK_F1:
							if (sdlEvent.type == SDL_KEYDOWN) {
								toggleFullscreen();
							}
						break;

						case SDLK_F5:
							if(!squareActive) {
								mySquare.draw();
								squareActive = true;
							} else {
								glClear(GL_COLOR_BUFFER_BIT);
								squareActive = false;
							}
							SDL_GL_SwapBuffers();
						break;

						case SDLK_F6:
							if(!cubeActive) {
								myCube.draw();
								cubeActive = true;
							} else {
								glClear(GL_COLOR_BUFFER_BIT);
								cubeActive = false;
							}
							SDL_GL_SwapBuffers();
						break;

						case SDLK_LEFT:
						case SDLK_a:
							if(squareActive) {
								mySquare.moveLeft();
								mySquare.draw();
							}
							SDL_GL_SwapBuffers();
						break;

						case SDLK_RIGHT:
						case SDLK_d:
							if(squareActive) {
								mySquare.moveRight();
								mySquare.draw();
							}
							SDL_GL_SwapBuffers();
						break;

						case SDLK_UP:
						case SDLK_w:
							if(squareActive) {
								mySquare.moveUp();
								mySquare.draw();
							}
							SDL_GL_SwapBuffers();
						break;

						case SDLK_DOWN:
						case SDLK_s:
							if(squareActive) {
								mySquare.moveDown();
								mySquare.draw();
							}
							SDL_GL_SwapBuffers();
						break;
						case SDLK_ESCAPE:
							if(sdlEvent.type == SDL_KEYDOWN) {
								// SDL_FreeSurface(hello);
								tidyQuit();
								return 0;
							}
						break;
					}
				break;

				case SDL_MOUSEMOTION:
					fprintf(stderr, "Current mouse position is (%d,%d)\n", sdlEvent.motion.x, sdlEvent.motion.y);
				break;
							
				// SDL_QUIT - when the user clicks close, we get a quit event
				case SDL_QUIT:
					tidyQuit();
					return 0;
				break;
			}

		}
	}
}